package com.czkt.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.czkt.pojo.BasDict;
import com.czkt.pojo.CstCustomer;
import com.czkt.pojo.SysUser;
import com.czkt.pojo.vo.SysUserVo;
import com.czkt.service.IBasDictService;
import com.czkt.service.ICstCustomerService;
import com.czkt.service.SysUserService;
import com.czkt.util.MD5Utils;
import com.czkt.util.PageUtile;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author liu
 * @since 2024-06-21
 */
@Controller
@RequestMapping("/customer")
public class CstCustomerController {

    @Autowired
    ICstCustomerService cstCustomerService;

    @Autowired
    IBasDictService basDictService;


    @Autowired
    SysUserService sysUserService;


    @RequestMapping("/list")
    public Object getList(@RequestParam(defaultValue = "1") Integer pageIndex,
                          @RequestParam(defaultValue = "2") Integer pageSize,
                          CstCustomer cstCustomer,
                          Model model) {
        IPage<CstCustomer> page = new Page<CstCustomer>(pageIndex, pageSize);



        QueryWrapper<CstCustomer> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .like(StringUtils.hasText(cstCustomer.getCustName()),CstCustomer::getCustName,cstCustomer.getCustName())
                .eq(StringUtils.hasText(cstCustomer.getCustRegion()),CstCustomer::getCustRegion,cstCustomer.getCustRegion());

        page  =cstCustomerService.page(page,queryWrapper);

        PageUtile pageUtile = new PageUtile();

        BeanUtils.copyProperties(page, pageUtile);


        //查询地区
        QueryWrapper<BasDict> queryWrapper1= new QueryWrapper<>();
        queryWrapper1.lambda().eq(BasDict::getDictType,"地区");


        model.addAttribute("dictList",  basDictService.list(queryWrapper1));
        model.addAttribute("page", pageUtile);
        model.addAttribute("custName", cstCustomer.getCustName());
        model.addAttribute("custRegion", cstCustomer.getCustRegion());


        return "customer/list";
    }







    /**
     * 定义跳转到新增页的方法
     */
    @RequestMapping("/user/add")
    public Object toAdd(Model model) {
//        model.addAttribute("roles", cstCustomerService.list());
        return "customer/add";
    }


    /**
     * 保存用户信息的方法
     */
    @PostMapping("/save")
    public Object saveUser(CstCustomer cstCustomer) {

        boolean result = cstCustomerService.saveOrUpdate(cstCustomer);
        if (result) {
            return "redirect:/customer/list";
        } else if (cstCustomer.getCustNo() == null) {
            return "redirect:/customer/add";
        } else {
            return "redirect:/customer/edit?usrId=" + cstCustomer.getCustNo();
        }
    }


    /**
     * 获取用户详细并跳转修改页面
     */
    @RequestMapping("/edit")
    public Object toUpdate(String customercId, Model model) {

        model.addAttribute("customer", cstCustomerService.getById(customercId));
        //查询地区
        QueryWrapper<BasDict> queryWrapper1= new QueryWrapper<>();
        queryWrapper1.lambda().eq(BasDict::getDictType,"地区");


        model.addAttribute("areas",  basDictService.list(queryWrapper1));

        //查询客户等级
        QueryWrapper<BasDict> customerLevel= new QueryWrapper<>();
        customerLevel.lambda().eq(BasDict::getDictType,"客户等级");


        model.addAttribute("customerLevel",  basDictService.list(customerLevel));
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<SysUser>();
        queryWrapper.lambda().eq(SysUser::getUsrRoleId, 2)
                .or().eq(SysUser::getUsrRoleId, 14);
        model.addAttribute("users", sysUserService.list(queryWrapper));


        return "customer/edit";
    }


    /**
     * 删除用户信息
     */
    @RequestMapping("/del/{custNo}")
    @ResponseBody
    public Object del(@PathVariable() String custNo , Model model) {
        Map resultMap = new HashMap();
        resultMap.put("delResult", cstCustomerService.removeById(custNo));
        return resultMap;
    }


}
